package net.sx.minemodule.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.IntDef;
import android.util.AttributeSet;
import android.view.View;

import net.sx.minemodule.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * TODO: document your custom view class.
 */
public class Dot extends View {

    public static final int START = 0;
    public static final int CENTER = 1;
    public static final int END = 2;
    int contentWidth;
    int contentHeight;
    private int color = Color.RED; // TODO: use a default from R.color...
    private int lineColor = Color.parseColor("#CCDDDDDD");
    private Paint paint;
    private Paint linePaint;
    private int position;
    private float strokeWidth = 1;
    private int style = 0;


    public Dot(Context context) {
        super(context);
        init(null, 0);
    }

    private void init(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.Dot, defStyle, 0);
        color = a.getColor(R.styleable.Dot_color, color);
        position = a.getInt(R.styleable.Dot_position, CENTER);
        lineColor = a.getColor(R.styleable.Dot_lineColor, lineColor);
        strokeWidth = a.getFloat(R.styleable.Dot_dotStrokeWidth, 1);
        style = a.getInt(R.styleable.Dot_dotStyle, Paint.Style.FILL.ordinal());
        a.recycle();

        // Set up a default TextPaint object
        paint = new Paint();
        paint.setColor(color);
        paint.setStyle(Paint.Style.values()[style]);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(strokeWidth);

        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(1);
    }

    public Dot(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public Dot(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int r = (contentWidth > contentHeight ? contentHeight : contentWidth) / 2 - 1;

        int lineStartY = 0;
        int lineStopY = contentHeight;
        int dotStartY = r;
        if (position == START) {
            lineStartY = r * 2 + getPaddingTop();
            lineStopY = contentHeight - getPaddingBottom();
            dotStartY = r + getPaddingTop();
        } else if (position == CENTER) {
            dotStartY = contentHeight / 2;
        } else if (position == END) {
            lineStopY = contentHeight - 2 * r - getPaddingBottom();
            dotStartY = contentHeight - r - getPaddingBottom();
        }
        canvas.drawLine(contentWidth / 2, lineStartY, contentWidth / 2, lineStopY, linePaint);
        canvas.drawCircle(contentWidth / 2, dotStartY, r, paint);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        contentWidth = width - paddingLeft - paddingRight;
        contentHeight = height - paddingTop - paddingBottom;
    }

    /**
     * 设置圆点的填充色
     *
     * @param color
     */
    public void setColor(int color) {
        this.color = color;
        paint.setColor(color);
        invalidate();
    }

    /**
     * 设置先的颜色
     *
     * @param color
     */
    public void setLineColor(int color) {
        this.lineColor = color;
        linePaint.setColor(lineColor);
        invalidate();
    }

    public void setStyle(Paint.Style style) {
        this.style = style.ordinal();
        paint.setStyle(style);
        invalidate();
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        paint.setStrokeWidth(strokeWidth);
        invalidate();
    }

    /**
     * 设置圆点的位置
     *
     * @param position
     */
    public void setPosition(@PositionMode int position) {
        this.position = position;
        invalidate();
    }

    @IntDef({
            START, CENTER, END
    })

    @Retention(RetentionPolicy.SOURCE)
    public @interface PositionMode {
    }

}
